// @flow
import React from 'react'
import { withRouter } from 'react-router'
import R from 'ramda'
import styles from './FaultMap.css'
import echarts from 'echarts/lib/echarts'
import 'echarts/extension/bmap/bmap'
import 'echarts/lib/component/title'
import 'echarts/lib/component/tooltip'
let styleJson = [{
  'featureType': 'railway',
  'elementType': 'all',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'highway',
  'elementType': 'all',
  'stylers': {
    'color': '#fdfdfd'
  }
}, {
  'featureType': 'highway',
  'elementType': 'labels',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'poi',
  'elementType': 'all',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'green',
  'elementType': 'all',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'subway',
  'elementType': 'all',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'arterial',
  'elementType': 'labels',
  'stylers': {
    'visibility': 'off'
  }
}, {
  'featureType': 'label',
  'elementType': 'labels.text.fill',
  'stylers': {
    'color': '#d8d7d3'
  }
}, {
  'featureType': 'label',
  'elementType': 'labels.icon',
  'stylers': {
    'visibility': 'off'
  }
}]

type Props = {
  data: Array<Object>,
  legend: Array<string>,
  history: Array<Object>
}
class FaultMap extends React.Component {
  props: Props
  draw: Function
  constructor (props) {
    super(props)
    this.draw = this.draw.bind(this)
  }
  componentDidMount () {
    this.draw(this.props)
  }
  componentWillReceiveProps (nextProps) {
    if (!R.equals(nextProps, this.props)) {
      this.draw(nextProps)
    }
  }
  draw (props) {
    const legend = props.data.map((d) => {
      return d.name
    })
    const component = this
    let myChart = echarts.init(this.refs.main)
    let option = {
      legend: {
        show: true,
        orient: 'vertical',
        bottom: '170px',
        right: '30px',
        itemWidth: 10,
        itemHeight: 10,
        data: legend
      },
      bmap: {
        center: [103.65, 36.76],
        zoom: 5,
        roam: true,
        mapStyle: {
          styleJson: styleJson
        }
      },
      // visualMap: {
      //   min: 0,
      //   max: 10,
      //   calculable: true,
      //   zlevel:444,
      //   bottom:160,
      //   left:10,
      //   inRange: {
      //     color: ['#50a3ba', '#eac736', '#d94e5d']
      //   },
      //   textStyle: {
      //     color: '#fff'
      //   }
      // },
      tooltip : {
        // show : true,
        shared: true,
        trigger: 'item',
        position: function (params) {
          // console.log(params);
        },
        formatter: function (params) {
          return '<p>故障名称：<span style="margin-left:5px">' + params.seriesName + '</span></p>' +
                 '<p>故障数量：<span style="margin-left:5px">' + params.value[2] + '</span></p>' +
                 '<p>所在客户：<span style="margin-left:5px">' + params.data.name + '</span></p>'
        }
      },
      series : props.data
    }
    myChart.setOption(option)
    // console.log(props.data);
    // console.log(myChart);
    // 添加缩放控件
    let map = myChart.getModel().getComponent('bmap').getBMap()
    map.addControl(new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT }))
    map.addControl(new BMap.NavigationControl())

    myChart.on('click', function (params) {
      const { history } = component.props
      const location = {
        pathname: '/device-card',
        state: {
          data: params.data,
          groupId: params.data.id,
          groupName: params.data.name,
          longitude: params.data.region.longitude,
          latitude: params.data.region.latitude,
          count: params.value[2]
        }
      }
      history.push(location)
    })
  }
  render () {
    return (
      <div className={styles['mainBg']}>
        <div ref='main' className={styles['main']} style={{
          position: 'absolute', top: '0', left: '0', right: '0', bottom: '0'
        }} />
      </div>
    )
  }
}

export default withRouter(FaultMap)
